package com.tl.khadmin.service.impl;
  
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Service;

import com.tl.khadmin.bean.OrderProduct;
import com.tl.khadmin.bean.OrderRefund;
import com.tl.khadmin.bean.SettlementBill;
import com.tl.khadmin.bean.Supplier;
import com.tl.khadmin.bean.dto.BillOrderDto;
import com.tl.khadmin.bean.dto.OrderRefundBillDto;
import com.tl.khadmin.mapper.OrderProductMapper;
import com.tl.khadmin.mapper.OrderRefundMapper;
import com.tl.khadmin.mapper.SettlementBillMapper;
import com.tl.khadmin.page.Page;
import com.tl.khadmin.service.BillIService;
import com.tl.khadmin.util.CommonUtil;
import com.tl.khadmin.util.Constant;
import com.tl.khadmin.util.PutParamUtil;  

@Service
public class BillServiceImpl implements BillIService{

	@Resource
	private SettlementBillMapper settlementBillMapper; 
	@Resource
	private OrderRefundMapper orderRefundMapper;
	@Resource
	private OrderProductMapper orderProductMapper;
	

	/**
	 * 分页查询售后退款订单账单
	 */
	@Override
	public Page<OrderRefundBillDto> showOrderRefundBillByPage(HttpSession session, Integer pageNum, Integer pageSize,
			Map<String, Object> params) {
		Page<OrderRefundBillDto> page=new Page<OrderRefundBillDto>(pageNum, pageSize);
        params.put("page", page);   
        Supplier supplier = (Supplier) session.getAttribute(Constant.USER_SUPPLIER);
        PutParamUtil.setParamIsInterior(supplier, params);	//设置查询供应商数据范围 
        params.put("refundStatus", 3);
        List<OrderRefundBillDto> list= orderRefundMapper.showOrderRefundBillByPage(params);  
		page.setResult(list);
		return page;
	}
	
	/**
	 * 供应商确认账单
	 */
	@Override
	public String confirm(HttpSession session, Integer id, Integer status) {
		SettlementBill bill = new SettlementBill();
		Supplier supplier = (Supplier) session.getAttribute(Constant.USER_SUPPLIER); 
		bill.setId(id); 
		bill.setConfirmedStatus(status);
		bill.setSupplierId(supplier.getId());
		return CommonUtil.returnMapResultByRows(settlementBillMapper.updateByPrimaryKeySelective(bill), 
				"提交成功", "提交失败");
	}
	
	/**
	 * 操作转账
	 */
	@Override
	public String transfer(HttpSession session, Integer id) {
		SettlementBill bill = new SettlementBill();
		Supplier supplier = (Supplier) session.getAttribute(Constant.USER_SUPPLIER);
		if(supplier.getIsInterior() != 1) {
			return CommonUtil.returnMapResultToJson("fail", "无权限");
		}
		bill.setId(id);
		//转账状态，1待转账，2已转账，3未转账，4转账失败
		bill.setTransferStatus(2); 
		return CommonUtil.returnMapResultByRows(settlementBillMapper.updateByPrimaryKeySelective(bill), 
				"提交成功", "提交失败");
	}
	
	@Override
	public Page<SettlementBill> showBillByPage(HttpSession session, Integer pageNum, 
			Integer pageSize, Map<String, Object> params) { 
		Page<SettlementBill> page=new Page<SettlementBill>(pageNum, pageSize);
        params.put("page", page);   
        Supplier supplier = (Supplier) session.getAttribute(Constant.USER_SUPPLIER);
        PutParamUtil.setParamIsInterior(supplier, params);	//设置查询供应商数据范围 
        List<SettlementBill> list= settlementBillMapper.showByPage(params);  
		page.setResult(list);
		return page;
	}

	/**
	 * 查询账单详情
	 */
	@Override
	public Map<String, Object> showBillDetail(Integer id) {
		Map<String, Object> result = new HashMap<String, Object>();
        result.put("bill", settlementBillMapper.selectByPrimaryKey(id));
        List<BillOrderDto> orderList = settlementBillMapper.findOrder(id);
        for(BillOrderDto order : orderList) {
        	BigDecimal refundAmount = new BigDecimal(0);	//该订单总退款金额
        	Map<String, Object> param = new HashMap<String, Object>();
        	param.put("orderId", order.getId());
        	List<OrderRefund> refundList = orderRefundMapper.listOrderRefund(param);
        	for(OrderRefund ref : refundList) {
        		refundAmount = refundAmount.add(new BigDecimal(ref.getAmount()));
        	}
        	order.setRefundAmount(refundAmount.doubleValue());
        	order.setTaxAmount(new BigDecimal(order.getOrderAmount()).multiply(new BigDecimal(Constant.TAX_RATE)).doubleValue());	//计算订单税金额
        
        	//获取到订单商品 
        	order.setOrderProductList(orderProductMapper.queryByOrderId(order.getId()));
        }
        
        result.put("orderList", settlementBillMapper.findOrder(id));
        return result;
	}

	@Override
	public List<OrderProduct> listOrderProduct(Integer orderId) { 
		return orderProductMapper.queryByOrderId(orderId);
	}



}
